Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
MongoDB Atlas search: sort by "searchScore"

I run the following aggregation pipeline and it works well:

[
  {
    $search: {
      text: {
        query: text,
        path: 'name',
        fuzzy: {
          maxEdits: 2,
          prefixLength: 0,
          maxExpansions: 256,
        },
      },
    },
  },
  {
    $limit: 10,
  },
  {
    $project: {
      _id: 1,
      name: 1,
      score: { $meta: 'searchScore' },
     },
  },
]

I can see that the score-field is present in my result and is correct. Now I want to sort the documents retrieved by the $meta-property searchScore. However, adding this step at the end of the pipeline:

{
  $sort: { score: { $meta: 'searchScore' }, _id: 1 },
}

Yields the following error:

MongoError: $meta sort by 'searchScore' metadata is not supported

How do I achieve this?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You need to add a new field that contains the result of $meta before sorting (which you already do) and then sort on that field, so you new pipeline would be:

...

{
    $project: {
      _id: 1,
      name: 1,
      score: { $meta: 'searchScore' }, // you are already adding the field here.
     },
},
{
   $sort: {
      score: -1, // use the new computed field here.
      _id: 1
   }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda